﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class FilePathMappingInstruction:InstructionBase
    {
        #region props
        private string _FolderPath;
        /// <summary></summary>
        public string FolderPath
        {
            get
            {
                return this._FolderPath;
            }
            set
            {
                this._FolderPath = value;
            }
        }

        private string _FileNameSearchPattern;
        /// <summary></summary>
        public string FileNameSearchPattern
        {
            get
            {
                return this._FileNameSearchPattern;
            }
            set
            {
                this._FileNameSearchPattern = value;
            }
        }

        private bool _IncludeSubFolders;
        /// <summary></summary>
        public bool IncludeSubFolders
        {
            get
            {
                return this._IncludeSubFolders;
            }
            set
            {
                this._IncludeSubFolders = value;
            }
        }

        private string _ConnStr;
        /// <summary></summary>
        public string ConnStr
        {
            get
            {
                return this._ConnStr;
            }
            set
            {
                this._ConnStr = value;
            }
        }

        private string _TblName;
        /// <summary></summary>
        public string TblName
        {
            get
            {
                return this._TblName;
            }
            set
            {
                this._TblName = value;
            }
        }

        private string _IDField;
        /// <summary></summary>
        public string IDField
        {
            get
            {
                return this._IDField;
            }
            set
            {
                this._IDField = value;
            }
        }

        private string _FilePathField;
        /// <summary></summary>
        public string FilePathField
        {
            get
            {
                return this._FilePathField;
            }
            set
            {
                this._FilePathField = value;
            }
        }

        private string _FileNameMatchPattern;
        /// <summary>regex pattern to grab id value from file name</summary>
        public string FileNameMatchPattern
        {
            get
            {
                return this._FileNameMatchPattern;
            }
            set
            {
                this._FileNameMatchPattern = value;
            }
        }

        private int _CatchIndex;
        /// <summary>default to 1</summary>
        public int CatchIndex
        {
            get
            {
                return this._CatchIndex;
            }
            set
            {
                this._CatchIndex = value;
            }
        }
        #endregion

        public FilePathMappingInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._FolderPath = string.Empty;
            this._FileNameSearchPattern = "*.*";
            this._IncludeSubFolders = true;
            this._ConnStr = string.Empty;
            this._TblName = string.Empty;
            this._IDField = string.Empty;
            this._FilePathField = string.Empty;
            this._FileNameMatchPattern = string.Empty;
            this._CatchIndex = 1;
        }

        public FilePathMappingInstruction(Guid componentID, Guid jobTicketID,
            string folderPath, string searchName, bool includeSubFolders,
            string connStr, string tblName, string idField, string filePathField,
            string matchPattern, int catchIndex) 
            : base(componentID, jobTicketID)
        {
            this._FolderPath = folderPath;
            this._FileNameSearchPattern = searchName;
            this._IncludeSubFolders = includeSubFolders;
            this._ConnStr = connStr;
            this._TblName = tblName;
            this._IDField = idField;
            this._FilePathField = filePathField;
            this._FileNameMatchPattern = matchPattern;
            this._CatchIndex = catchIndex;
        }

        #region Overrides of InstructionBase

        /// <summary>serialize settings to xml</summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode,"FolderPath",this.FolderPath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "NameSearchPattern", this.FileNameSearchPattern);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IncludeSubFolders", this.IncludeSubFolders.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ConnStr", this.ConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TableName", this.TblName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "IDField", this.IDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FilePathField", this.FilePathField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "NameMatchPattern", this.FileNameMatchPattern);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "CatchIndex", this.CatchIndex.ToString());
            
            return xNode;
        }

        /// <summary>
        /// instantiated from xml
        /// "&lt;Instructions&gt;&lt;Instruction&gt;&lt;/Instruction&gt;&lt;/Instruction&gt;", must
        /// instantiate from first child of root node
        /// </summary>
        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                FilePathMappingInstruction instruction = new FilePathMappingInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.FolderPath = XmlDataUtil.GetAttributeValue(xNode, "FolderPath", "");
                instruction.FileNameSearchPattern = XmlDataUtil.GetAttributeValue(xNode, "NameSearchPattern", "*.*");
                instruction.IncludeSubFolders =
                    bool.Parse(XmlDataUtil.GetAttributeValue(xNode, "IncludeSubFolders",
                                                             true.ToString()));
                instruction.ConnStr = XmlDataUtil.GetAttributeValue(xNode, "ConnStr", "");
                instruction.TblName = XmlDataUtil.GetAttributeValue(xNode, "TableName", "");
                instruction.IDField = XmlDataUtil.GetAttributeValue(xNode, "IDField", "");
                instruction.FilePathField = XmlDataUtil.GetAttributeValue(xNode, "FilePathField", "");
                instruction.FileNameMatchPattern = XmlDataUtil.GetAttributeValue(xNode, "NameMatchPattern", "");
                instruction.CatchIndex = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "CatchIndex", "1"));

                
                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
